package scm.erp.core.domain;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lombok.Data;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
@Data
public class Pg<T, S> {
    Logger log = LoggerFactory.getLogger("Pg");

    @NotNull(message = "pageSize不能为空")
    private Integer pageSize;
    @NotNull(message = "pageNum不能为空")
    private Integer pageNum;
    private Map<String, String> pageOrder;
    //    private List<T> pageData;

    private S pageData;


    @JsonIgnore
    private Page<T> page;
    private PageInfo<T> pageInfo;

    public Pg<T, S> page(Page<T> page) {
        this.page = page;
        return this;
    }

    public Pg<T, S> startPage() {
        Page<T> page = PageHelper.startPage(this.getPageNum(), this.getPageSize());
        log.info("分页参数设置完毕:pageNum-{},pageSize:{}", this.getPageNum(), this.getPageSize());
        return this.page(page);
    }

    public Pg<T, S> selectPage(ISelect<T> select) {
        this.pageInfo = new PageInfo<>(select.selectData());
        return this;
    }

    public Pg<T, S> closePage() {
        this.page.close();
        return this;
    }

    public interface ISelect<T> {
        List<T> selectData();
    }

}
